package com.rew.canteenbox.bases;

import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;

/**
 * 带有滑动功能的模板类.
 * <p>
 * Created by HuiWen Ren on 2016/10/25.
 */
public class SlideActivity extends BaseActivity
        implements View.OnTouchListener, GestureDetector.OnGestureListener {

    //手指向右滑动时的最小速度.
    protected static final int XSPEED_MIN = 200;

    //手指向右滑动时的最小距离.
    protected static final int XDISTANCE_MIN = 150;

    //记录手指按下时的横坐标.
    protected float xDown;

    //记录手指移动时的横坐标.
    protected float xMove;

    //用于计算手指滑动的速度.
    protected VelocityTracker mVelocityTracker;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        createVelocityTracker(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                xDown = event.getRawX();
                break;
            case MotionEvent.ACTION_MOVE:
                xMove = event.getRawX();
                //活动的距离.
                int distanceX = (int) (xMove - xDown);
                //获取顺时速度.
                int xSpeed = getScrollVelocity();
                // 当滑动的距离大于我们设定最小距离,
                // 且滑动的瞬间速度大于我们设定的速度时,
                // 视为有效滑动.
                if (distanceX > XDISTANCE_MIN && xSpeed > XSPEED_MIN) {
                    // ΔX > 0 向右滑动.
                    right();
                } else if (-1 * distanceX > XDISTANCE_MIN && xSpeed > XSPEED_MIN) {
                    // ΔX < 0 向左滑动.
                    left();
                }
                break;
            case MotionEvent.ACTION_UP:
                recycleVelocityTracker();
                break;
            default:
                break;
        }
        return true;
    }

    /*
        创建VelocityTracker对象,
        并将触摸content界面的滑动事件加入到VelocityTracker当中.
     */
    protected void createVelocityTracker(MotionEvent event) {
        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        }
        mVelocityTracker.addMovement(event);
    }

    /*
         回收VelocityTracker对象.
     */
    protected void recycleVelocityTracker() {
        mVelocityTracker.recycle();
        mVelocityTracker = null;
    }

    /*
        获取手指在content界面滑动的速度.
     */
    protected int getScrollVelocity() {
        mVelocityTracker.computeCurrentVelocity(1000);
        int velocity = (int) mVelocityTracker.getXVelocity();
        return Math.abs(velocity);
    }


    /*
        以下为默认要求重构的无关函数及设置.
     */
    @Override
    public boolean onDown(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent motionEvent) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent motionEvent) {

    }

    @Override
    public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        return false;
    }

    protected void left() {
    }

    protected void right() {
    }
}
